﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain;
using RBAC.Server.Interface;
using Swork03_28.Api.Application.Commands.UserCommand;
using System.Security.Claims;
using static System.Net.Mime.MediaTypeNames;

namespace RBAC.Poject.Controllers
{
    /// <summary>
    /// 用户信息控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UsersController : ControllerBase
    {
        private readonly IUserServer _userServer;
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="userServer"></param>
        public UsersController(IUserServer userServer, IMediator mediator)
        {
            _userServer = userServer;
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取省份信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ProVicnce>> GetProVicnces()
        {
            return await _userServer.GetProVicnces();
        }

        /// <summary>
        /// 获取市区信息
        /// </summary>
        /// <param name="id">省份编号</param>
        /// <returns>符合条件的数据</returns>
        [HttpGet]
        public async Task<List<Citys>> GetCity(int id)
        {
            return await _userServer.GetCity(id);
        }

        /// <summary>
        /// 获取区信息
        /// </summary>
        /// <param name="id">市份编号</param>
        /// <returns>符合条件的数据</returns>
        [HttpGet]
        public async Task<List<District>> GetDistrict(int id)
        {
            return await _userServer.GetDistrict(id);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="userInfo">用户实体</param>
        /// <returns>状态码</returns>
        [HttpPost]
        public async Task<int> AddUserInfo(UserInfo userInfo)
        {
            return await _userServer.AddUserInfo(userInfo);
        }

        [HttpPost]
        public async Task<int> test(testCommand command)
        {
            var res = HttpContext.User;
            var UserId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            command.UserId =int.Parse( UserId);
            return await mediator.Send(command);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <returns>符合条件的数据</returns>
        [HttpGet]
        public async Task<IActionResult> GetUserInfos(int pageIndex, int pageSize)
        {

             var data = await _userServer.GetUserInfos(pageIndex, pageSize);
            return Ok(new
            {
                data.pageCount,
                data.totalCount,
                data.list
            });
        }

        /// <summary>
        /// 反填用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<UserInfo> GetUser(int id)
        {
            return await _userServer.GetUser(id);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfo">用户实体</param>
        /// <returns>状态码</returns>
        [HttpPut]
        public async Task<int> Upd(UserInfo userInfo)
        {
            return await _userServer.Upd(userInfo);
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns>状态码</returns>
        [HttpPut]
        public async Task<int> Del(int id)
        {
            return await _userServer.Del(id);
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        [HttpPost]
        public int UserAdds(string? users)
        {
            return _userServer.UserAdds(users);
        }
    }
}
